<template>
  <div class="container">
    <marquee behavior="alternate" scrollamount="3"
      ><span
        style="
          font-weight: bolder;
          font-size: 30px;
          color: rgba(240, 5, 5, 0.6);
        "
        >{{ this.ServiceStation.slogan }}</span
      ></marquee>
    <div class="form-list">
      <div class="alit-right">
        <p style="flex: 1"></p>
        <p style="color: #0000008a">{{ this.ServiceStation.name }}</p>
      </div>
      <van-form @submit="onSubmit">
        <van-cell-group inset>
          <van-field
            v-model="phone"
            type="phone"
            name="手机号"
            label="手机号"
            placeholder="请输入手机号"
            :rules="[{ required: true, message: '请填写手机号' }]"
            required
            @change="findUserInfo"
          />
          <van-field
            v-model="name"
            name="姓名"
            label="姓名"
            placeholder="请输入姓名"
            :rules="[{ required: true, message: '请填写用户名' }]"
            required
          />

          <van-field
            v-model="village"
            name="所属村"
            label="所属村"
            placeholder="请输入您现在的所属村"
          />
          <van-field
            v-model="dept"
            name="所在单位"
            label="所在单位"
            placeholder="请输入您现在所在单位"
          />
        </van-cell-group>
        <div style="margin: 20px">
          <van-button round block type="primary" native-type="submit">
            下一步
          </van-button>
        </div>
      </van-form>
    </div>
    <van-dialog v-model="show" title="签到成功" @confirm="handleConfirm">
      <!-- <div style="display: flex;
  justify-content: center;"> -->
      <div class="fcc">
        <span class="f12"
          >欢迎光临
          <span style="color: blue">{{ this.ServiceStation.name }}</span></span
        >
      </div>
      <van-cell title="手机号" :value="this.phone" />
      <van-cell title="姓名" :value="this.name" />
      <van-cell title="所属村" :value="this.village" />
      <van-cell title="所在单位" :value="this.dept" />
    </van-dialog>
  </div>
</template>

<script>
import { getAxios2 } from "@/utils/request2";
import { getServiceStation } from "@/api/fgbm";
import { formatDate3 } from "@/utils/getDate.js";

export default {
  data() {
    return {
      info: "",
      name: "",
      phone: "",
      village: "",
      dept: "",
      tag: "",
      time: "",
      axios2: {},
      ServiceStation: {}, //站点信息
      show: false,
    };
  },
  mounted() {},
  async created() {
    this.tag = this.$route.query.tag;
    this.axios2 = await getAxios2();
    this.ServiceStation = await getServiceStation(this.axios2, this.tag);
  },
  methods: {
    onSubmit() {
      //验证名字的正则表达式
      const regName = /^[\u4E00-\u9FA5]{2,4}$/;
      // 验证手机号的正则表达式
      var regPhone = /^1[3-9]\d{9}$/;
      if (!regName.test(this.name)) {
        return this.$toast("请输入合法的姓名！");
      }
      if (!regPhone.test(this.phone)) {
        return this.$toast("请输入合法的手机号！");
      }
      // this.time = formatDate2(new Date());
      this.time = formatDate3(new Date());
      const userdata = {
        memberName: this.name,
        memberId: this.phone,
        village: this.village,
        workPlace: this.dept,
        createTime: this.time,
        tag: this.tag,
      };
      this.saveStudyMsg(userdata);
    },

    // 保存学习用户信息
    async saveStudyMsg(data) {
      // const res = await this.$axios.post('http://120.79.234.223:8558/app/study/saveSignIn',data)
      const res = await this.axios2.post(
        "https://fgbm.zatsafety.com/app/study/saveSignIn",
        data
      );
      this.show = res.msg == "success";
    },
    handleConfirm() {
      this.show = false;
      this.phone = "";
      this.name = "";
      this.village = "";
      this.dept = "";
      this.time = "";
    },
    async findUserInfo() {
      var self = this;
      let params = new FormData(); //创建form对象
      params.append("params[memberId]", self.phone);
      const res = await self.axios2.post(
        "https://fgbm.zatsafety.com/app/study/memberList",
        params
      );
      let data = res.data[0];
      if (data) {
        self.name = data.memberName;
        self.village = data.village;
        self.dept = data.workPlace;
      } else {
        self.name = "";
        self.village = "";
        self.dept = "";
      }
    },
  },
};
</script>

<style lang="less" scoped>
.container {
  .form-list {
    width: 90%;
    margin: 20px auto;
    height: 80vh;
    background-color: #fff;
    border-radius: 10px;
    box-shadow: 0 0 5px #ccc;
  }
}
.alit-right {
  position: relative;
  display: flex;
  box-sizing: border-box;
  width: 100%;
  padding: 10px 16px;
  overflow: hidden;
  color: #323233;
  font-size: 14px;
  line-height: 24px;
  background-color: #fff;
}
</style>